<?php
require("../services/class/conexion.php");
$link = mysql_connect($SERVIDOR, $USUARIO, $PASSWORD);
mysql_select_db($BASE, $link);
mysql_query("SET NAMES 'utf8'");

include "../libs/libchart/classes/libchart.php";

header("Content-type: image/png");

$_REQUEST['fecha'] = $_REQUEST['fecha'] = explode("/", $_REQUEST['fecha']);
$_REQUEST['fecha'] = $_REQUEST['fecha'][2] . "-" . $_REQUEST['fecha'][1] . "-" . $_REQUEST['fecha'][0];

$dataSet = new XYSeriesDataSet();

$OS_IMP = new XYDataSet();
$SELECT = mysql_query("SELECT cod_obra_social, tabla, descripcion FROM obras_sociales ORDER BY cod_obra_social DESC");
WHILE ($CONS = mysql_fetch_array($SELECT))
{
	$SEL_OS = mysql_query("SELECT SUM( importe ) AS importe FROM os_" . $CONS['tabla'] . " WHERE estado = 'C' AND DATE_FORMAT(fecha, '%Y%m') = DATE_FORMAT('" . $_REQUEST['fecha'] . "', '%Y%m')");
	$CON_OS = mysql_fetch_array($SEL_OS);
	$OS_IMP->addPoint(new Point("( " . $CONS['cod_obra_social'] . " )", $CON_OS['importe']));
}

$OS_COB = new XYDataSet();
$SELECT = mysql_query("SELECT cod_obra_social, tabla, descripcion FROM obras_sociales ORDER BY cod_obra_social DESC");
WHILE ($CONS = mysql_fetch_array($SELECT))
{
	$SEL_OS = mysql_query("SELECT SUM( cobrado ) AS cobrado FROM os_" . $CONS['tabla'] . " WHERE estado = 'C' AND DATE_FORMAT(fecha, '%Y%m') = DATE_FORMAT('" . $_REQUEST['fecha'] . "', '%Y%m')");
	$CON_OS = mysql_fetch_array($SEL_OS);
	$OS_COB->addPoint(new Point("( " . $CONS['cod_obra_social'] . " )", $CON_OS['cobrado']));
}

$dataSet->addSerie("Cobrado", $OS_COB);
$dataSet->addSerie("Presentado", $OS_IMP);

$chart = new HorizontalBarChart(750, 250);
$chart->setDataSet($dataSet);

$_REQUEST['fecha'] = explode("-", $_REQUEST['fecha']);
$_REQUEST['fecha'] = $_REQUEST['fecha'][1] . "/" . $_REQUEST['fecha'][0];
$chart->setTitle("Diferencia entre Presentado y Cobrado de la fecha: " . $_REQUEST['fecha']);

$chart->render();
?>